
const user = {
    state: {    //存储的字段
        userId: '',  //用户id
        username: '',  //用户账号
        avator: '',   //用户的头像
    },
    getters: {  //getter方法
        userId: state => {
            let userId = state.userId;
            if(!userId){
                //如果没有，从window中获取
                userId = JSON.parse(window.sessionStorage.getItem("userId"));
            }
            return userId;
        },
        username: state => {
            let username = state.username;
            if(!username){
                //如果没有，从window中获取
                username = JSON.parse(window.sessionStorage.getItem("username"));
            }
            return username;
        },
        avator: state => {
            let avator = state.avator;
            if(!avator){
                //如果没有，从window中获取
                avator = JSON.parse(window.sessionStorage.getItem("avator"));
            }
            return avator;
        }
    },
    mutations: {    //setter方法
        setUserId:(state, userId) => {
            state.userId = userId;
            window.sessionStorage.setItem("userId", JSON.stringify(userId));
        },
        setUsername:(state, username) => {
            state.username = username;
            window.sessionStorage.setItem("username", JSON.stringify(username));
        },
        setAvator:(state, avator) => {
            state.avator = avator;
            window.sessionStorage.setItem("avator", JSON.stringify(avator));
        }
    }
};
export default user;